﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using System;
using TomNet.App.Model.DB;
using TomNet.App.Model.DTO;
using TomNet.AspNetCore.Data;
using TomNet.AspNetCore.Mvc;
using TomNet.Data;
using TomNet.SqlSugarCore.Entity;

namespace TomNet.App.WebApi.Controllers
{
    /// <summary>
    /// 商品
    /// </summary>
    [AllowAnonymous]
    public class ProductController : ApiController
    {
        private readonly IUnitOfWork _unitOfWork; 
        public ProductController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork; 
        }
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public AjaxResult List(int pageIndex = 1, int pageSize = 3)
        {
            //ProductListDto
            int total = 0;
            var data = _unitOfWork.DbContext.Queryable<Product>().Select(m => new 
            {
                BType = m.BType,
                ExpectYield = 0,
                Price = m.Price,
                Id = m.Id,
                ProductUnit = m.ProductUnit,
                Quantity = m.Quantity,
                Term = m.Term

            }).ToPageList(pageIndex, pageSize, ref total);
            return new AjaxResult("允许访问", AjaxResultType.Success, new { Data = data, Total = total });
        }
        /// <summary>
        /// 商品详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public AjaxResult Info(int Id)
        {
            //ProductListDto
            int total = 0;
            var data = _unitOfWork.DbContext.Queryable<Product>().Where(m=>m.Id==Id).Select(m => new 
            {
                BType = m.BType,
                ExpectYield = 0,
                Price = m.Price,
                Id = m.Id,
                ProductUnit = m.ProductUnit,
                Quantity = m.Quantity,
                Term = m.Term 
            }).Single();
            return new AjaxResult("允许访问", AjaxResultType.Success, new { Data = data, Total = total });
        }
    }
}